package com.ruint.core.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.annotation.SuppressLint;

/**
 * com.ruint.core.utils.DateUtils
 * 
 * @author ruint <br/>
 *         create at 2014-11-25 上午11:10:50
 */
@SuppressLint("SimpleDateFormat")
public class DateUtils {

  public static String getBetween(long time) {
    long timedeta = Math.abs(System.currentTimeMillis() / 1000 - time);
    Calendar now = Calendar.getInstance();
    long ms_now = now.getTimeInMillis();
    long ms = 1000 * (now.get(Calendar.HOUR_OF_DAY) * 3600 + now.get(Calendar.MINUTE) * 60 + now.get(Calendar.SECOND));
    if (ms_now - time * 1000 < ms) {
      if (timedeta < 60) {
        return "now";
      } else if (timedeta < 3600) {
        return Math.round(timedeta / 60) + "mins ago";
      } else {
        return toTime(time);
      }
    } else if (ms_now - time * 1000 < (ms + 24 * 3600 * 1000)) {
      return "yesterday";
    } else if (ms_now - time * 1000 < (ms + 24 * 3600 * 1000 * 2)) {
      return "2 days ago";
    } else {
      return toDateWithYear(time);
    }
  }

  public static String getImBetween(long time) {
    Calendar now = Calendar.getInstance();
    long ms_now = now.getTimeInMillis();
    long ms = 1000 * (now.get(Calendar.HOUR_OF_DAY) * 3600 + now.get(Calendar.MINUTE) * 60 + now.get(Calendar.SECOND));
    if (ms_now - time * 1000 < ms) {
      return toTime(time);
    } else if (ms_now - time * 1000 < (ms + 24 * 3600 * 1000)) {
      return "yesterday" + toTime(time);
    } else if (ms_now - time * 1000 < (ms + 24 * 3600 * 1000 * 2)) {
      return "2 days ago" + toTime(time);
    } else {
      return toDateAndTime(time);
    }
  }

  public static int getBetweenMin(long start, long end) {
    return ((int) ((end - start) / (long) (60))) + 1;
  }

  public static String toDateAndTime(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(time).toString();
  }

  public static String toDateAndTimeNoSec(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    return sdf.format(time).toString();
  }

  public static String toDate(long time) {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdfY = new SimpleDateFormat("yyyy");
    SimpleDateFormat sdfMD = new SimpleDateFormat("MM-dd");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    if (sdfY.format(time * 1000).toString().equals(String.valueOf(c.get(Calendar.YEAR)))) {
      return sdfMD.format(time * 1000).toString();
    } else {
      return sdf.format(time * 1000).toString();
    }
  }

  public static String toDateWithYear(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(time * 1000).toString();
  }

  public static String toTime(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    return sdf.format(time * 1000).toString();
  }

  public static boolean isTheSameDay(long time1, long time2) {
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c1.setTimeInMillis(time1 * 1000);
    c2.setTimeInMillis(time2 * 1000);
    return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
        && (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH));
  }

  /**
   * 
   * @param time
   * @return yyyy-MM-dd HH:mm:ss(String)
   */
  public static String toRightTime(Long time) {
    if (time == null) {
      return null;
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(time * 1000).toString();
  }

  public static String toFullDate(long time) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(time * 1000).toString();
  }

  /**
   * 将当前时间格式化输出
   * 
   * @return
   */
  public static String formatCurrentDate(String format) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    return dateFormat.format(new Date());
  }

  public static String format3DaysLaterDate(String format) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.DAY_OF_MONTH, 3);
    return dateFormat.format(calendar.getTime());
  }

  public static String formatTimestamp(long time, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = new Date(time);
    return sdf.format(date).toString();
  }

  public static long formatStringToTimestamp(String time, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = null;
    try {
      date = sdf.parse(time);
      return date.getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return 0;
  }

  public static boolean compare_date_string(String mintime, String maxtime, String time) {
    SimpleDateFormat df = new SimpleDateFormat("hh:mm");
    boolean b = false;
    try {
      Date d1 = df.parse(mintime);
      Date d2 = df.parse(maxtime);
      Date d = df.parse(time);
      if (d.getTime() > d1.getTime() && d.getTime() < d2.getTime()) {
        return true;
      }
    } catch (Exception e) {
    }
    return b;
  }

  public static String getDiaryDate(String oritime) {
    Calendar dayc1 = new GregorianCalendar();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date daystart = null;
    try {
      daystart = df.parse(oritime);
    } catch (ParseException e) {
    }
    dayc1.setTime(daystart);

    if (dayc1.get(Calendar.YEAR) == Calendar.getInstance().get(Calendar.YEAR)) {
      if (dayc1.get(Calendar.MONTH) == Calendar.getInstance().get(Calendar.MONTH)) {
        if (dayc1.get(Calendar.DAY_OF_MONTH) == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
          if (dayc1.get(Calendar.HOUR_OF_DAY) == Calendar.getInstance().get(Calendar.HOUR_OF_DAY)) {
            if (dayc1.get(Calendar.MINUTE) == Calendar.getInstance().get(Calendar.MINUTE)) {
              return Calendar.getInstance().get(Calendar.SECOND) - dayc1.get(Calendar.SECOND) + "second ago";
            } else {
              return Calendar.getInstance().get(Calendar.MINUTE) - dayc1.get(Calendar.MINUTE) + "min ago";
            }
          } else {
            return Calendar.getInstance().get(Calendar.HOUR_OF_DAY) - dayc1.get(Calendar.HOUR_OF_DAY) + "hour ago";
          }
        } else {
          return Calendar.getInstance().get(Calendar.DAY_OF_MONTH) - dayc1.get(Calendar.DAY_OF_MONTH) + "day ago";
        }
      } else {
        if (Calendar.getInstance().get(Calendar.MONTH) - dayc1.get(Calendar.MONTH) > 3) {
          return Calendar.getInstance().get(Calendar.MONTH) - dayc1.get(Calendar.MONTH) + "month ago";
        } else {
          return dayc1.get(Calendar.MONTH) + "-" + dayc1.get(Calendar.DAY_OF_MONTH);
        }
      }
    } else {
      return Calendar.getInstance().get(Calendar.YEAR) - dayc1.get(Calendar.YEAR) + "year ago";
    }
  }

  public static String getDiaryTime(String date) {
    Calendar calendar = new GregorianCalendar(Locale.CHINA);
    Calendar sysCalendar = Calendar.getInstance(Locale.CHINA);
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date day = null;
    try {
      day = format.parse(date);
    } catch (ParseException e) {
    }
    calendar.setTime(day);
    int hour = Integer.parseInt(date.split(" ")[1].split(":")[0]);

    int curYear = sysCalendar.get(Calendar.YEAR);
    int curMonth = sysCalendar.get(Calendar.MONTH);
    int curDay = sysCalendar.get(Calendar.DAY_OF_MONTH);
    int curHour = sysCalendar.get(Calendar.HOUR_OF_DAY);
    int curMinute = sysCalendar.get(Calendar.MINUTE);
    int curSecond = sysCalendar.get(Calendar.SECOND);

    int msYear = calendar.get(Calendar.YEAR);
    int msMonth = calendar.get(Calendar.MONTH);
    int msDay = calendar.get(Calendar.DAY_OF_MONTH);
    int msHour = hour;
    int msMinute = calendar.get(Calendar.MINUTE);
    int msSecond = calendar.get(Calendar.SECOND);

    if (curYear == msYear) {

      if (curMonth == msMonth) {

        if (curDay == msDay) {

          if (curHour == msHour) {

            if (curMinute == msMinute) {

              date = msSecond + "second ago";

            } else {

              if (curMinute - msMinute == 1 && curSecond + (60 - msSecond) < 60) {
                date = curSecond + (60 - msSecond) + "second ago";
              } else {
                date = curMinute - msMinute + "min ago";
              }

            }

          } else {

            if (curHour - msHour == 1 && curMinute + (60 - msMinute) < 60) {
              date = curMinute + (60 - msMinute) + "min ago";
            } else {
              date = curHour - msHour + "hour ago";
            }

          }

        } else {

          if (curDay - msDay == 1 && curHour + (24 - msHour) < 24) {
            date = curHour + (24 - msHour) + "hour ago";
          } else {
            date = curDay - msDay + "day ago";
          }

        }

      } else {

        if (curMonth - msMonth == 1 && curDay + (30 - msDay) < 30) {

          date = curDay + (30 - msDay) + "day ago";
        } else {
          date = curMonth - msMonth + "month ago";
        }
      }

    } else {

      if (curYear - msYear == 1 && curMonth + (12 - msMonth) < 12) {

        date = curMonth + (12 - msMonth) + "month ago";

      } else {

        date = curYear - msYear + "year ago";
      }
    }
    return date.startsWith("-") ? (msYear + "-" + msMonth + "-" + msDay) : date;
  }
}
